from django.shortcuts import render

# Create your views here.
from . import models
##数据库的页面呈现 ，后端

# def index(request):  #单词的正确article
#     article =models.Artical.objects.get(pk=1)  #主数据库中主键pk为1，其他键也可以，类似sql的select
#     return render(request,'firstReduceAPP/index.html',{'article':article})
#
#获取所有文章链接代码

def index(request):  #h获取所有文章列表
    articles = models.Artical.objects.all()  #all返回的是ajango的一个集合，类似python 列表的东西
    return render(request,"firstReduceAPP/index.html",{'articles':articles})

def article_page(request,article_id):
    article = models.Artical.objects.get(pk = article_id)
    return render(request,"firstReduceAPP/article_page.html",{'article':article})

def edit_page(request,article_id):
    if str(article_id) =='0':
        return render(request,'fiestRrduceApp/edit_page.html')
    article = models.Artical.objects.get(pk=article_id)
    return render(request,"firstReduceApp/edit_page.html",{'article':article})

def edit_action(request):
    title =request.POST.get('title','TITLE')
    content =request.POST.get('content','CONTENTcd')
    models.Artical.objects.create(titlie =title,content=content)
    articles = models.Artical.objects.all()  # all返回的是ajango的一个集合，类似python 列表的东西
    return render(request, "firstReduceAPP/index.html", {'articles': articles})
